/*
* @Author: Karry
* @Date:   2017-10-19 20:24:12
* @Last Modified by:   Karry
* @Last Modified time: 2017-10-19 21:13:01
*/

require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide       = require('page/common/nav-side/index.js');
var _db           = require('util/db.js');
var _user         = require('service/user-service.js');

// page 逻辑部分
var page = {
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        // 初始化左侧菜单
        navSide.init({
            name : 'user-pass-update'
        });
    },
    bindEvent : function(){
        var _this = this;
        // 点击提交按钮后的动作 btn-sublime
        $(document).on('click','.btn-submit',function(){
            var userInfo = {
                password            : $.trim($('#password').val()),
                passwordNew         : $.trim($('#password-new').val()),
                passwordConfirm     : $.trim($('#password-confirm').val())
            },
            validateResult = _this.validateForm(userInfo);
            if (validateResult.status) {
                // 这里是更改用户信息
                _user.updatePassword({
                    passwordOld : userInfo.password,
                    passwordNew : userInfo.passwordNew
                }, function(res, msg){
                    _db.successTips(msg);
                }, function(errMsg){
                    _db.errorTips(errMsg);
                });
            }else{
                _db.errorTips(validateResult.msg);
            }
        });
    },
    // 验证字段信息
    validateForm : function(formData){
        var result = {
            status : false,
            msg    : ''
        };
        // 验证原密码是否为空
        if (!_db.validate(formData.password,'require')) {
            result.msg = '原密码不能为空';
            return result;
        }
        // 验证新密码长度
        if (!formData.passwordNew || formData.passwordNew.length < 6) {
            result.msg = '新密码长度不能少于六位';
            return result;
        }
        // 验证密保不能为空
        if (formData.passwordNew !== formData.passwordConfirm) {
            result.msg = '两次密码输入不一致';
            return result;
        }
        // 通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    }
};
$(function(){
    page.init();
});